package ie.dit.reg08.manager;

import java.util.Vector;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Trial {
	String id;
	String condition;
	Node trial;
	Domain domain;
	AttributeSet description;
	String WORD_STRING;
	
	public String getID() {
		return id;
	}
	
	public String getCondition() {
		return condition;
	}
	
	public Domain getDomain() {
		return domain;
	}
	
	public AttributeSet getDescription() {
		return description;
	}
	
	public String getWordString(){
		return WORD_STRING;
	}
	
	/*
	 * returns true if the corpus description for this trial is distinguishing
	 */
	public boolean IsDistinguishing() {
		Vector<Entity> vDistractors = domain.distractorSet(description.getHashtableDescription());
		if(!(vDistractors.isEmpty())) {
			return false;
		}
		return true;
	}

	
	/*
	 * returns true if the corpus description for this trial contains an undefined 'other' attribute
	 */
	public boolean otherAttribute() {
		return description.containsOther();
	}
	
	/*
	 * returns true if there is > 1 target in this trial
	 */
	public boolean isPlural() {
		return domain.isPlural();
	}
	
	public Trial(Node n) {
		if (n.hasChildNodes()) {
			trial = n;
			if (trial.hasChildNodes()) {
				NamedNodeMap attributes = trial.getAttributes();
				id = attributes.getNamedItem("ID").getNodeValue();
				condition = attributes.getNamedItem("CONDITION").getNodeValue();
				
				NodeList nl = trial.getChildNodes();
				
				for(int i=0; i< nl.getLength(); i++) {
					if(nl.item(i).getNodeName().equals("DOMAIN")) {
						domain = new Domain(nl.item(i));						
					} else if (nl.item(i).getNodeName().equals("WORD-STRING")) {
						WORD_STRING = nl.item(i).getFirstChild().getNodeValue();
					} else if (nl.item(i).getNodeName().equals("ANNOTATED-WORD-STRING")) {
					} else if (nl.item(i).getNodeName().equals("ATTRIBUTE-SET")) {
						description = new AttributeSet(nl.item(i));
					}
				}
				
				//System.out.println(description.getStringDescription());
				//System.out.println(WORD_STRING);
				//System.out.println();

			} else {
				System.err.println("trial: trial has no children!");
			}
		} else {
			System.err.println("trial: n has no children!");
			System.err.println("n = : ");
			System.err.println(n.toString());
		}
	}
	
	public void outputTrialDescription() {
		System.out.println("Trial id = " + id + " condition = " + condition);
		domain.printDomain();
		System.out.println(description.getStringDescription());
		System.out.println("***************************************************************************************");
	}
}
